package com.attilax.autoup;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

import javax.ws.rs.NotFoundException;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;
import com.attilax.compress.ZipUtil;
import com.attilax.core.functionImp;
import com.attilax.io.pathx;
import com.attilax.net.ftp.RunnableImp;
import com.attilax.net.ftp.lambdaRunner;
import com.attilax.shell.CliShellProcessService;
import com.attilax.shell.ProcessUtil;
import com.attilax.util.ExUtil;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class autoupdate {

	public static void main(String[] args) {
		// up

		String zipFilePath = "c:\\0000upzip\\firdir.zip";
		zipFilePath = "c:\\0000upzip\\honurse WebContent v2 s510.zip";
		zipFilePath = "c:\\d\\dd2\\up511.zip";
		try {
			List<String> ret = new autoupdate().exeup(zipFilePath,"utf8");
			System.out.println(ret);
		} catch (Exception e) {
			System.out.println(JSON.toJSONString(e));
		}
		

		System.out.println("--f");

	}

	String upziprztlog = "";
	@SuppressWarnings("all")
	public Map debuginfo = Maps.newConcurrentMap();

	
	List<String> exeup(final String zipFilePath, String charset_console_ret,final String upzippath) {
		final List<String> ret_cmd_rzt_li = Lists.newArrayList();
		ZipUtil.unzipV3(zipFilePath, upzippath,new Function() {

			@Override
			public Object apply(Object input) {
				ret_cmd_rzt_li.add("upzip file准备解压文件:" + input);
			 
				String zipfileDir = new File(zipFilePath).getParent();
				 
				ret_cmd_rzt_li.add("upzip file to dst解压文件到目录:" + upzippath +File.separator + input);
				return null;
			}
		});
		debuginfo.put("ret_cmd_rzt_li", ret_cmd_rzt_li);

		String opdir =(upzippath);// new File(zipFilePath).getParent();

		String charsetName = charset_console_ret;// charsetName = "gbk";

		String subdir = pathx.filemainname(new File(zipFilePath).getName());
		String shellext = pathx.shellex();
		String lastcmd = opdir + File.separator + subdir + File.separator + "update." + shellext;
		if (!new File(lastcmd).exists())
			ExUtil.throwExV4("notexistex,file:" + lastcmd, ret_cmd_rzt_li);
		 
		
		if (pathx.isLinux()) {
			// add shell file auth
		
			String chAuthCmd = " chmod 777  " + lastcmd;
			ret_cmd_rzt_li.add(">>>>>>"+chAuthCmd);
			Process Process1 = ProcessUtil.start(chAuthCmd, opdir);
			// Thread.sleep(1000);
			String stdout_2str_ByIoutil = CliShellProcessService.stdout_2str_ByIoutil(Process1, charsetName);
			ret_cmd_rzt_li.add(stdout_2str_ByIoutil);
			String erroutput2strByIoutil = CliShellProcessService.Erroutput2strByIoutil(Process1, charsetName);
			if (erroutput2strByIoutil.trim().length() > 10) {
				//debuginfo.put("chmod ret ", stdout_2str_ByIoutil);
				//ret_cmd_rzt_li.add(">>>>>>chmod ret:");
				ret_cmd_rzt_li.add(erroutput2strByIoutil);
				ExUtil.throwExV4("chmod 777 ex,file；" + lastcmd, ret_cmd_rzt_li);
				 
			}

		}
	//	debuginfo.put("cmd", lastcmd);
		ret_cmd_rzt_li.add(">>>>>>"+lastcmd);

		Process Process1 = ProcessUtil.start(lastcmd, opdir);
		// Thread.sleep(1000);
		String stdout_2str_ByIoutil = CliShellProcessService.stdout_2str_ByIoutil(Process1, charsetName);
		ret_cmd_rzt_li.add(stdout_2str_ByIoutil);
//		System.out.println(stdout_2str_ByIoutil);
//		System.out.println("---------errout::is now错误信息");
		String erroutput2strByIoutil = CliShellProcessService.Erroutput2strByIoutil(Process1, charsetName);
		ret_cmd_rzt_li.add(erroutput2strByIoutil);
		//System.out.println(erroutput2strByIoutil);
	//	String ret = Joiner.on("\r\n").join(ret_cmd_rzt_li);
		return ret_cmd_rzt_li;
	}

	
	
	
	private Object String (String upzippath) {
		// TODO Auto-generated method stub
		return null;
	}




	List<String> exeup(final String zipFilePath, String charset_console_ret) {
		final List<String> ret_cmd_rzt_li = Lists.newArrayList();
		ZipUtil.unzipZipoisit(zipFilePath, new Function() {

			@Override
			public Object apply(Object input) {
				ret_cmd_rzt_li.add("upzip file准备解压文件:" + input);
			 
				String zipfileDir = new File(zipFilePath).getParent();
				 
				ret_cmd_rzt_li.add("upzip file to dst解压文件到当前目录:" + zipfileDir + "/" + input);
				return null;
			}
		});
		debuginfo.put("ret_cmd_rzt_li", ret_cmd_rzt_li);

		String opdir = new File(zipFilePath).getParent();

		String charsetName = charset_console_ret;// charsetName = "gbk";

		String subdir = pathx.filemainname(new File(zipFilePath).getName());
		String shellext = pathx.shellex();
		String lastcmd = opdir + File.separator + subdir + File.separator + "update." + shellext;
		if (!new File(lastcmd).exists())
			ExUtil.throwExV4("notexistex,file:" + lastcmd, ret_cmd_rzt_li);
		 
		
		if (pathx.isLinux()) {
			// add shell file auth
		
			String chAuthCmd = " chmod 777  " + lastcmd;
			ret_cmd_rzt_li.add(">>>>>>"+chAuthCmd);
			Process Process1 = ProcessUtil.start(chAuthCmd, opdir);
			// Thread.sleep(1000);
			String stdout_2str_ByIoutil = CliShellProcessService.stdout_2str_ByIoutil(Process1, charsetName);
			ret_cmd_rzt_li.add(stdout_2str_ByIoutil);
			String erroutput2strByIoutil = CliShellProcessService.Erroutput2strByIoutil(Process1, charsetName);
			if (erroutput2strByIoutil.trim().length() > 10) {
				//debuginfo.put("chmod ret ", stdout_2str_ByIoutil);
				//ret_cmd_rzt_li.add(">>>>>>chmod ret:");
				ret_cmd_rzt_li.add(erroutput2strByIoutil);
				ExUtil.throwExV4("chmod 777 ex,file；" + lastcmd, ret_cmd_rzt_li);
				 
			}

		}
	//	debuginfo.put("cmd", lastcmd);
		ret_cmd_rzt_li.add(">>>>>>"+lastcmd);

		Process Process1 = ProcessUtil.start(lastcmd, opdir);
		// Thread.sleep(1000);
		String stdout_2str_ByIoutil = CliShellProcessService.stdout_2str_ByIoutil(Process1, charsetName);
		ret_cmd_rzt_li.add(stdout_2str_ByIoutil);
//		System.out.println(stdout_2str_ByIoutil);
//		System.out.println("---------errout::is now错误信息");
		String erroutput2strByIoutil = CliShellProcessService.Erroutput2strByIoutil(Process1, charsetName);
		ret_cmd_rzt_li.add(erroutput2strByIoutil);
		//System.out.println(erroutput2strByIoutil);
	//	String ret = Joiner.on("\r\n").join(ret_cmd_rzt_li);
		return ret_cmd_rzt_li;
	}

}
